﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using System.Text;
using NHibernate.Criterion;
using NHibernate;
using System.Collections;
using ZLCMS.Data.Model;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Config;
using Newtonsoft.Json;
using System.Reflection;
using ZLCMS.Business.Util;
using ZLCMS.Business.TranferParam;
using DotNet.Common.TransferParam;
using ZLCMS.Web.SystemManage.Control;
using ZLCMS.Commands.Data;


namespace ZLCMS.Web.SystemManage.Basics.ProductCategory
{
    public partial class EditCategorySKU : System.Web.UI.Page
    {
        protected FrameHelper _framePage;
        protected List<ProductCustomData> _groupsDb;
        protected ShopCategory _cateDb;
        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化DWZ控件
            _framePage = new FrameHelper(this);

            //初始化全局变量值
            InitializeGlobalValue();

            //初始化控件值
            InitializeContent();

            if (IsPostBack)
            {
                Save();
            }
        }

        protected void InitializeContent()
        {
            //页面控件数据

            //页面控件值
            if (!IsPostBack)
            {
            }
        }

        protected void InitializeGlobalValue()
        {
            string cid;

            try
            {
                if (!IsPostBack)
                {
                    if (string.IsNullOrWhiteSpace(Request["cid"]))
                    {
                        _cid.Text = null;
                        return;
                    }
                    else
                    {
                        cid = Request["cid"];
                        _cid.Text = cid;
                        using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                        {
                            _cateDb = session.Get<ShopCategory>(int.Parse(cid));
                        }
                    }
                }
                else
                {
                    if (string.IsNullOrWhiteSpace(_cid.Text))
                    {
                        return;
                    }
                    cid = _cid.Text;
                    using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                    {
                        _cateDb = session.Get<ShopCategory>(int.Parse(cid));
                    }
                }
                if (_cateDb.CustomField != null)
                {
                    //_groupsDb = cbc_list.SetSelectedValue(_cateDb.CustomField);
                }

                ParamKit.Bind("ProductCustomGroup", cbc_list.DataList, null, null, null, null, null);

                if (_cateDb == null)
                {
                    throw new Exception("找不到对应信息.");
                }
            }
            catch (System.Exception ex)
            {
                _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                return;
            }
        }

        protected void Save()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                try
                {
                    if (_cateDb == null)
                    {
                        throw new Exception("找不到对应信息.");
                    }
                    else
                    {
                        UpdateData(_cateDb.Id, cbc_list.SelectedValue, session);
                        _cateDb.CustomField = cbc_list.SelectedValue;
                        session.Update(_cateDb);
                        
                    }

                    session.Flush();
                }
                catch (System.Exception ex)
                {
                    _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                    return;
                }
            }
            // 刷新 editparamgroup 页面
            _framePage.Alert(new ReturnCode()
            {
                statusCode = 200,
                message = "操作成功!",
                callbackType = "closeCurrent",
                navTabId = "shopaddredit"
            });
        }

        protected void UpdateData(int id,string sku, ISession session)
        {
            session.CreateQuery("update ShopCategory set CustomField=:acc where ParentId = :id").SetParameter("acc",sku).SetParameter("id", id).ExecuteUpdate();
            session.Flush();
        }

    }
}
